package app.imttf.online.backing.login;

import app.imttf.online.adfextensions.ADFUtils;
import app.imttf.online.adfextensions.JSFUtils;

import app.imttf.online.backing.admin.user.UserData;

import app.imttf.online.model.OnlineTtfAMImpl;

import app.imttf.online.model.view.admin.access.UserOlSupplierViewImpl;

import app.imttf.online.model.view.admin.access.UserOlSupplierViewRowImpl;

import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.binding.OperationBinding;

public class LoginBean {

    private RichInputText inputLogin, inputPassword;
    private String _action;
    private final String MSG_USER_NO_SUP_ACCESS = "User tidak memiliki hak supplier untuk di proses";

    public String loginAction() {
        OnlineTtfAMImpl onlineTtfAM =
            (OnlineTtfAMImpl)ADFUtils.getApplicationModuleForDataControl("OnlineTtfAMDataControl");

        _action = null;

        OperationBinding login = ADFUtils.findOperation("authenticateUser");
        Map m = (Map)login.execute();

        try {
            if (m != null && !m.isEmpty()) {

                HashMap userAccess = (HashMap)m.get("UserAccess");
                String fullName = (String)m.get("FullName");
                String userNameLogin = (String)m.get("UserName");
                String password = (String)m.get("Password");
                String companyName = (String)m.get("CompanyName");
                String userType = (String)m.get("userType");
                
                String supCodeList = findSupCode(onlineTtfAM, userNameLogin);

                UserData userData =
                    (UserData)JSFUtils.resolveExpression("#{UserData}");
                userData.setLoggedIn(Boolean.TRUE);
                userData.setUserAccess(userAccess);
                userData.setFullName(fullName);
                userData.setUserNameLogin(userNameLogin);
                userData.setPassword(password);
                userData.setCompanyName(companyName);
                userData.setUserType(userType);
                userData.setSupCode(supCodeList);
                userData.setSupCodeLabel(supCodeList.replace("'", ""));
                userData.setSupTipe("Y"); // Y=PKP; N=Non PKP

                OperationBinding _menuAMSession =
                    ADFUtils.findOperation("setLoginToSession_MenuAM");
                _menuAMSession.execute();

                OperationBinding _onlineTtfAMSession =
                    ADFUtils.findOperation("setLoginToSession_OnlineTtfAM");
                _onlineTtfAMSession.execute();

                _action = "success";
            } else {
                _action = "errlogin";
            }
        } catch (Exception e) {
            _action = "errglobal";
        }

        return _action;
    }

    public void setInputLogin(RichInputText inputLogin) {
        this.inputLogin = inputLogin;
    }

    public RichInputText getInputLogin() {
        return inputLogin;
    }

    public void setInputPassword(RichInputText inputPassword) {
        this.inputPassword = inputPassword;
    }

    public RichInputText getInputPassword() {
        return inputPassword;
    }

    private String findSupCode(OnlineTtfAMImpl onlineTtfAM, String userName) {
        String supCode = "";
        long userOlSupplierRowCount;

        UserOlSupplierViewImpl userOlSupplierView =
            onlineTtfAM.getUserOlSupplierView1();
        userOlSupplierView.setuserName(userName);
        userOlSupplierView.executeQuery();

        userOlSupplierRowCount = userOlSupplierView.getEstimatedRowCount();

        if (userOlSupplierRowCount > 0) {

            for (int y = 0; y < userOlSupplierRowCount; y++) {
                UserOlSupplierViewRowImpl userOlSupplierRow =
                    (UserOlSupplierViewRowImpl)userOlSupplierView.getRowSetIterator().next();
                if (y == 0) {
                    supCode = "'" + userOlSupplierRow.getKdSupplier().trim() + "'";
                } else { 
                    supCode = supCode + ", '" + userOlSupplierRow.getKdSupplier().trim() + "'";
                }
            }
            
        } else {
            showError(MSG_USER_NO_SUP_ACCESS);
        }

        return supCode;
    }
    
    public String showError(String msg) {
            FacesMessage fm = new FacesMessage(msg);
            /**
                 * set the type of the message.
                 * Valid types: error, fatal,info,warning
                 */
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, fm);
            return null;
        }
}
